//
//  GQHDriverAuthorizationController.m
//  Xanthium
//
//  Created by GuanQinghao on 31/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHDriverAuthorizationView.h"
#import "GQHCustomCell.h"

#pragma mark Controller
#import "GQHDriverAuthorizationController.h"
#import "GQHDriverAuthorizationSubController.h"
#import "GQHModelController.h"
#import "GQHLengthController.h"
#import "GQHPlateController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHDriverAuthorizationController () <GQHDriverAuthorizationViewDelegate> {
    
#pragma mark Data
    NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHDriverAuthorizationView *rootView;
}

@end


@implementation GQHDriverAuthorizationController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHDriverAuthorizationView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"userId"];
    [self fetchDriverProfile];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"司机认证信息";
    [self.qh_rightButton setHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
            
        case 0: {
            
            // 联系人
            [self qh_alertWithTitle:@"请输入联系人" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入联系人";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"empName"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:0];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 1: {
            
            // 联系电话
            [self qh_alertWithTitle:@"请输入联系电话" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入联系电话";
                textField.keyboardType = UIKeyboardTypeNumberPad;
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"phoneNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:1];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 2: {
            
            // 身份证号码
            [self qh_alertWithTitle:@"请输入身份证号码" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入身份证号码";
                textField.keyboardType = UIKeyboardTypeNumberPad;
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"dirIdCard"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:2];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 3: {
            
            // 从业资格证号码
            [self qh_alertWithTitle:@"请输入从业资格证号码" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入从业资格证号码";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"qualCerNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:3];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 4: {
            
            // 所有人
            [self qh_alertWithTitle:@"请输入所有人" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入所有人";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"carUser"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:4];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 5: {
            
            // 道路经营许可证号
            [self qh_alertWithTitle:@"请输入道路经营许可证号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入道路经营许可证号";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"roadTraBuLicNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:5];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 6: {
            
            // 道路运输许可证号
            [self qh_alertWithTitle:@"请输入道路运输许可证号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入道路运输许可证号";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"roadTraCerNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:6];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 7: {
            
            // 车牌号
            [self qh_alertWithTitle:@"请输入车牌号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入车牌号";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"licPlateNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:7];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 8: {
            
            // 挂车牌照号
            [self qh_alertWithTitle:@"请输入挂车牌照号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入挂车牌照号";
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"trailerRegistrationNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:8];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 9: {
            
            // 车型
            GQHModelController *modelController = [[GQHModelController alloc] init];
            modelController.qh_modelBlock = ^(NSString *modelID, NSString *modelName) {
                
                if (modelID) {
                    
                    [parameters setObject:modelID forKey:@"vehicleclassificationcode"];
                    [rootView.qh_contentArray qh_safetySetObject:modelName atIndex:9];
                    [rootView.qh_tableView reloadData];
                }
            };
            modelController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:modelController animated:YES];
        }
            break;
        case 10: {
            
            // 车长
            GQHLengthController *lengthController = [[GQHLengthController alloc] init];
            lengthController.qh_lengthBlock = ^(NSString *lengthID, NSString *lengthName) {
                
                if (lengthID) {
                    
                    [parameters setObject:lengthID forKey:@"carLength"];
                    [rootView.qh_contentArray qh_safetySetObject:lengthName atIndex:10];
                    [rootView.qh_tableView reloadData];
                }
            };
            lengthController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:lengthController animated:YES];
        }
            break;
        case 11: {
            
            // 车牌类型
            GQHPlateController *plateController = [[GQHPlateController alloc] init];
            plateController.qh_plateBlock = ^(NSString *plateID, NSString *plateName) {
                
                if (plateID) {
                    
                    [parameters setObject:plateID forKey:@"carType"];
                    [rootView.qh_contentArray qh_safetySetObject:plateName atIndex:11];
                    [rootView.qh_tableView reloadData];
                }
            };
            plateController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:plateController animated:YES];
        }
            break;
        case 12: {
            
            // 载重
            [self qh_alertWithTitle:@"请输入载重" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入载重";
                textField.keyboardType = UIKeyboardTypeNumberPad;
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"carLoad"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:12];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        default:
            break;
    }
}

- (void)qh_nextPage
{
    NSArray * keys = @[@"empName",
                       @"phoneNum",
                       @"dirIdCard",
                       @"qualCerNum",
                       @"carUser",
                       @"roadTraCerNum",
                       @"roadTraBuLicNum",
                       @"licPlateNum",
                       @"trailerRegistrationNum"
                       ];
    for (int i = 0; i < keys.count; i ++)
    {
        [parameters setObject:rootView.qh_contentArray[i] forKey:keys[i]];
    }
    [parameters setObject:rootView.qh_contentArray.lastObject forKey:@"carLoad"];
    
    if ([[parameters objectForKey:@"userId"] length] == 0) {
        
        [self showToastWithText:@"用户信息出错"];
        return;
    }
    else if ([[parameters objectForKey:@"empName"] length] == 0) {
        
        [self showToastWithText:@"请输入联系人姓名"];
        return;
    }
    else if ([[parameters objectForKey:@"phoneNum"] length] == 0) {
        
        [self showToastWithText:@"请输入联系电话"];
        return;
    }
    else if (![[parameters objectForKey:@"phoneNum"] qh_isValidMobile]) {
        
        [self showToastWithText:@"请输入有效的联系电话"];
        return;
    }
    
//    if (![parameters objectForKey:@"dirIdCard"]) {
//
//        [self showToastWithText:@"请输入身份证号码"];
//        return;
//    }
//
//    if (![[parameters objectForKey:@"dirIdCard"] qh_isValidIdentityCardNo]) {
//
//        [self showToastWithText:@"请输入有效的身份证号码"];
//        return;
//    }
    
    else if ([[parameters objectForKey:@"qualCerNum"] length] == 0) {
        
        [self showToastWithText:@"请输入从业资格证号码"];
        return;
    }
    else if ([[parameters objectForKey:@"carUser"] length] == 0) {
        
        [self showToastWithText:@"请输入所有人"];
        return;
    }
    else if ([[parameters objectForKey:@"roadTraCerNum"] length] == 0) {
        
        [self showToastWithText:@"请输入道路运输许可证号"];
        return;
    }
    else if ([[parameters objectForKey:@"roadTraBuLicNum"] length] == 0) {
        
        [self showToastWithText:@"请输入道路经营许可证号"];
        return;
    }
    else if ([[parameters objectForKey:@"licPlateNum"] length] == 0) {
        
        [self showToastWithText:@"请输入车牌号"];
        return;
    }
    else if ([[parameters objectForKey:@"vehicleclassificationcode"] length] == 0) {
        
        [self showToastWithText:@"请选择车型"];
        return;
    }
    else if ([[parameters objectForKey:@"carLength"] length] == 0) {
        
        [self showToastWithText:@"请选择车长"];
        return;
    }
    else if ([[parameters objectForKey:@"carType"] length] == 0) {
        
        [self showToastWithText:@"请选择车牌类型"];
        return;
    }
    else if ([[parameters objectForKey:@"carLength"] length] == 0) {
        
        [self showToastWithText:@"请选择车长"];
        return;
    }
    else if ([[parameters objectForKey:@"carLoad"] length] == 0) {
        
        [self showToastWithText:@"请输入车辆载重"];
        return;
    }
    else if (0 == [[parameters objectForKey:@"carLoad"] integerValue] ) {
        
        [self showToastWithText:@"车辆载重不能为0"];
        return;
    }
    
    GQHDriverAuthorizationSubController *driverAuthorizationSubController = [[GQHDriverAuthorizationSubController alloc] init];
    driverAuthorizationSubController.qh_parameters = parameters;
    driverAuthorizationSubController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:driverAuthorizationSubController animated:YES];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchDriverProfile {
    
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:USER_GET_ID forKey:@"userId"];
    
    NSMutableString *URLString = [NSMutableString stringWithString:[NSString qh_APIURLString]];
    [URLString appendString:@"/authInformation/1/diverMsgView"];
    
    [GQHNetHelper qh_requestGET:URLString parameters:para success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            
            /// 联系人
            NSString *empName = [data objectForKey:@"empName"];
            if (![empName isKindOfClass:[NSNull class]] && empName && empName.length > 0) {
                
                [parameters setObject:empName forKey:@"empName"];
                [rootView.qh_contentArray qh_safetySetObject:empName atIndex:0];
            }
            
            // 联系电话
            NSString *phoneNum = [data objectForKey:@"phoneNum"];
            if (![phoneNum isKindOfClass:[NSNull class]] && phoneNum && phoneNum.length > 0) {
                
                [parameters setObject:phoneNum forKey:@"phoneNum"];
                [rootView.qh_contentArray qh_safetySetObject:phoneNum atIndex:1];
            }
            
            // 身份证号码
            NSString *dirIdCard = [data objectForKey:@"dirIdCard"];
            if (![dirIdCard isKindOfClass:[NSNull class]] && dirIdCard && dirIdCard.length > 0) {
                
                [parameters setObject:dirIdCard forKey:@"dirIdCard"];
                [rootView.qh_contentArray qh_safetySetObject:dirIdCard atIndex:2];
            }
            
            // 从业资格证号码
            NSString *qualCerNum = [data objectForKey:@"qualCerNum"];
            if (![qualCerNum isKindOfClass:[NSNull class]] && qualCerNum && qualCerNum.length > 0) {
                
                [parameters setObject:qualCerNum forKey:@"qualCerNum"];
                [rootView.qh_contentArray qh_safetySetObject:qualCerNum atIndex:3];
            }
            
            // 所有人
            NSString *carUser = [data objectForKey:@"carUser"];
            if (![carUser isKindOfClass:[NSNull class]] && carUser && carUser.length > 0) {
                
                [parameters setObject:carUser forKey:@"carUser"];
                [rootView.qh_contentArray qh_safetySetObject:carUser atIndex:4];
            }
            
            // 道路经营许可证号
            NSString *roadTraBuLicNum = [data objectForKey:@"roadTraBuLicNum"];
            if (![roadTraBuLicNum isKindOfClass:[NSNull class]] && roadTraBuLicNum && roadTraBuLicNum.length > 0) {
                
                [parameters setObject:roadTraBuLicNum forKey:@"roadTraBuLicNum"];
                [rootView.qh_contentArray qh_safetySetObject:roadTraBuLicNum atIndex:5];
            }
            
            // 道路运输许可证号
            NSString *roadTraCerNum = [data objectForKey:@"roadTraCerNum"];
            if (![roadTraCerNum isKindOfClass:[NSNull class]] && roadTraCerNum && roadTraCerNum.length > 0) {
                
                [parameters setObject:roadTraCerNum forKey:@"roadTraCerNum"];
                [rootView.qh_contentArray qh_safetySetObject:roadTraCerNum atIndex:6];
            }
            
            // 车牌号
            NSString *licPlateNum = [data objectForKey:@"licPlateNum"];
            if (![licPlateNum isKindOfClass:[NSNull class]] && licPlateNum && licPlateNum.length > 0) {
                
                [parameters setObject:licPlateNum forKey:@"licPlateNum"];
                [rootView.qh_contentArray qh_safetySetObject:licPlateNum atIndex:7];
            }
            
            // 挂车牌照号
            NSString *trailerRegistrationNum = [data objectForKey:@"trailerRegistrationNum"];
            if (![trailerRegistrationNum isKindOfClass:[NSNull class]] && trailerRegistrationNum && trailerRegistrationNum.length > 0) {
                
                [parameters setObject:trailerRegistrationNum forKey:@"trailerRegistrationNum"];
                [rootView.qh_contentArray qh_safetySetObject:trailerRegistrationNum atIndex:8];
            }
            
            // 车型
            NSString *vehicleclassificationcode = [data objectForKey:@"vehicleclassificationcode"];
            NSString *vehicleclassificationcodeKey = [data objectForKey:@"vehicleclassificationcodeKey"];
            
            if (![vehicleclassificationcode isKindOfClass:[NSNull class]] && vehicleclassificationcode && vehicleclassificationcode.length > 0) {
                
                [parameters setObject:vehicleclassificationcodeKey forKey:@"vehicleclassificationcode"];
                [rootView.qh_contentArray qh_safetySetObject:vehicleclassificationcode atIndex:9];
            }
            
            // 车长
            NSString *carLength = [data objectForKey:@"carLength"];
            NSString *carLengthKey = [data objectForKey:@"carLengthKey"];
            if (![carLength isKindOfClass:[NSNull class]] && carLength && carLength.length > 0) {
                
                [parameters setObject:carLengthKey forKey:@"carLength"];
                [rootView.qh_contentArray qh_safetySetObject:carLength atIndex:10];
            }
            
            // 车牌类型
            NSString *licenseplatetypecode = [data objectForKey:@"licenseplatetypecode"];
            NSString *licenseplatetypecodeKey = [data objectForKey:@"licenseplatetypecodeKey"];
            if (![licenseplatetypecode isKindOfClass:[NSNull class]] && licenseplatetypecode && licenseplatetypecode.length > 0) {
                
                [parameters setObject:licenseplatetypecodeKey forKey:@"carType"];
                [rootView.qh_contentArray qh_safetySetObject:licenseplatetypecode atIndex:11];
            }
            
            // 载重
            NSString *carLoad = [data objectForKey:@"carLoad"];
            if (![carLoad isKindOfClass:[NSNull class]] && carLoad && carLoad.length > 0) {
                
                [parameters setObject:carLoad forKey:@"carLoad"];
                [rootView.qh_contentArray qh_safetySetObject:carLoad atIndex:12];
            }
            
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
